// jjwm/writeAddress/writeAddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [{
        name: '男',
        value: '先生'
      },
      {
        name: '女',
        value: '女士'
      },
    ],
    gender: "",
    SHDZ: {}
  },

  /**
   * 自定义事件
   */
  SetADDRESS: function(e) {

  },
  formSubmit: function(e) {
    console.log(e.detail.value)
    if (e.detail.value.username == "" && e.detail.value.phone == "" && e.detail.value.floor == "" && this.data.gender == "" && this.data.SHDZ.title == "点击选择") {
      wx.showToast({
        title: '注册信息不能为空',
        icon: "none",
        duration: 2000
      })
    } else {
      const db = wx.cloud.database()
      db.collection("address").add({
        data: {
          username: e.detail.value.username,
          phone: e.detail.value.phone,
          floor: e.detail.value.floor,
          gender: this.data.gender,
          location: this.data.SHDZ.title
        },
        success: (res) => {
          this.setData({
            SHDZ: {
              title: "点击选择"
            }
          })
          wx.removeStorage({
            key: 'SHDZ-ADDRESS',
            success: function(res) {},
          })
          wx.navigateBack({
            delta: 1
          })
        }
      })
    }
  },
  radioChange: function(e) {
    this.setData({
      gender: e.detail.value
    })
  },
  GoBack: function() {

  },
  jumpSearchAddress: function(e) {
    wx.navigateTo({
      url: '../searchAddress/searchAddress',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.getStorage({
      key: 'SHDZ-ADDRESS',
      success: (res) => {
        console.log(res.data.hasOwnProperty("title"))
        if (res.data.hasOwnProperty("title")) {
          this.setData({
            SHDZ: res.data
          })
        }
      },
      fail: (err) => {
        this.setData({
          SHDZ: {
            title: "点击选择"
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    console.log(1)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})